/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddressPanel.java
 *
 * Created on 19 Jan 2011, 11:41:00 AM
 */
package za.co.pas.business.view.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import za.co.pas.business.data.Address;
import za.co.pas.business.data.Country;
import za.co.pas.business.data.Province;
import za.co.pas.business.view.model.combo.CountryComboModel;
import za.co.pas.business.view.model.combo.ProvinceComboModel;

/**
 *
 * @author alabuschagne
 */
public class AddressPanel extends javax.swing.JPanel
{

    private CountryComboModel countryComboModel;
    private ProvinceComboModel provinceComboModel;

    private AddressPanel()
    {
    }

    /** Creates new form AddressPanel */
    public AddressPanel(String whatAddress, String street1Title, String street2Title)
    {
        countryComboModel = new CountryComboModel();
        provinceComboModel = new ProvinceComboModel();
        initComponents();
        setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), whatAddress, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        this.streetLabel1.setText(street1Title);
        this.streetLabel2.setText(street2Title);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        streetLabel1 = new javax.swing.JLabel();
        streetLabel2 = new javax.swing.JLabel();
        streetTextField1 = new javax.swing.JTextField();
        streetTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        suburbTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        countryComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        provinceComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cityTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        codeTextField = new javax.swing.JTextField();
        spacerLabel = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Address", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        streetLabel1.setText("Street Address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(streetLabel1, gridBagConstraints);

        streetLabel2.setText("Street Address:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(streetLabel2, gridBagConstraints);

        streetTextField1.setText("     ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(streetTextField1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(streetTextField2, gridBagConstraints);

        jLabel1.setText("Suburb:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(suburbTextField, gridBagConstraints);

        jLabel2.setText("Country:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(jLabel2, gridBagConstraints);

        countryComboBox.setModel(countryComboModel);
        countryComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                countryComboBoxItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(countryComboBox, gridBagConstraints);

        jLabel3.setText("Province / State:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 10, 5);
        add(jLabel3, gridBagConstraints);

        provinceComboBox.setModel(provinceComboModel);
        provinceComboBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                provinceComboBoxMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 10);
        add(provinceComboBox, gridBagConstraints);

        jLabel4.setText("City:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(jLabel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(cityTextField, gridBagConstraints);

        jLabel5.setText("Postal Code / Zip:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(jLabel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 10);
        add(codeTextField, gridBagConstraints);

        spacerLabel.setText("                                        ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 10);
        add(spacerLabel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void countryComboBoxItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_countryComboBoxItemStateChanged
    {//GEN-HEADEREND:event_countryComboBoxItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED)
        {
            updateCountryComboBox();
        }
    }//GEN-LAST:event_countryComboBoxItemStateChanged

    private void newProvince()
    {
        
    }
    private void provinceComboBoxMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_provinceComboBoxMouseReleased
    {//GEN-HEADEREND:event_provinceComboBoxMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    newProvince();
                }
            });
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_provinceComboBoxMouseReleased

    private void updateCountryComboBox()
    {
        //load the other combo box
        Country c = (Country) this.countryComboBox.getSelectedItem();
        if (c == null)
        {
            this.provinceComboModel.setCountry(null);
            this.provinceComboBox.setSelectedIndex(-1);
        }
        else
        {
            this.provinceComboModel.setCountry(c);
        }
        this.provinceComboBox.updateUI();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cityTextField;
    private javax.swing.JTextField codeTextField;
    private javax.swing.JComboBox countryComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox provinceComboBox;
    private javax.swing.JLabel spacerLabel;
    private javax.swing.JLabel streetLabel1;
    private javax.swing.JLabel streetLabel2;
    private javax.swing.JTextField streetTextField1;
    private javax.swing.JTextField streetTextField2;
    private javax.swing.JTextField suburbTextField;
    // End of variables declaration//GEN-END:variables

    public String getStreet1()
    {
        return this.streetTextField1.getText();
    }

    public String getStreet2()
    {
        return this.streetTextField2.getText();
    }

    public String getSuburb()
    {
        return this.suburbTextField.getText();
    }

    public String getCity()
    {
        return this.cityTextField.getText();
    }

    public String getCode()
    {
        return this.codeTextField.getText();
    }

    public Province getProvince()
    {
        return (Province) this.provinceComboBox.getSelectedItem();
    }

    public Country getCountry()
    {
        return (Country) this.countryComboBox.getSelectedItem();
    }

    public void set(Address address)
    {
        if (address == null)
        {
            this.streetTextField1.setText("");
            this.streetTextField2.setText("");
            this.suburbTextField.setText("");
            this.cityTextField.setText("");
            this.codeTextField.setText("");
            this.countryComboBox.setSelectedIndex(-1);
            updateCountryComboBox();
        }
        else
        {
            this.streetTextField1.setText(address.getLine1());
            this.streetTextField2.setText(address.getLine2());
            this.suburbTextField.setText(address.getSuburb());
            this.cityTextField.setText(address.getCity());
            this.codeTextField.setText(address.getCode());
            this.countryComboBox.setSelectedItem(address.getCountry());
            updateCountryComboBox();
            this.provinceComboBox.setSelectedItem(address.getProvince());
        }
    }
}
